public class hanoi {
    public static void main(String[] args) {
        int n = 3; // 汉诺塔的层数
        hanoi(n, 'A', 'B', 'C',1);
    }

    // 将n个盘子从from柱移动到to柱，借助于temp柱
    public static void hanoi(int n, char from, char temp, char to,int j) {

        if (n == 1) {
            // 只有一个盘子时，直接将其从from柱移动到to柱
            System.out.println("n:"+n+" from:"+from+" temp:"+temp+" to:"+to+" 第"+j+"层");
            System.out.println("Move disk 1 from " + from + " to " + to);
            System.out.println("返回上一层");
        } else {
            // 将n-1个盘子从from柱移动到temp柱，借助于to柱
            j++;
            System.out.println("第"+(j-1)+"层 进入 hanoi("+n+" - 1, "+from+", "+to+", "+temp+","+j+");");
            hanoi(n - 1, from, to, temp,j);
            j--;
            // 将第n个盘子从from柱移动到to柱
            System.out.println("n:"+n+" from:"+from+" temp:"+temp+" to:"+to+" 第"+j+"层");
            System.out.println("Move disk " + n + " from " + from + " to " + to);
            // 将n-1个盘子从temp柱移动到to柱，借助于from柱
            j++;
            System.out.println("第"+(j-1)+"层 进入 hanoi("+n+" - 1, "+temp+", "+from+", "+to+","+j+");");
            hanoi(n - 1, temp, from, to,j);
            j--;
            System.out.println("返回上一层");
        }
    }
}
